<?php

namespace App\Http\Controllers\Admin\Upload;

use Illuminate\Http\Request;
use App\Http\Controllers\Admin\BaseController;
use DB;
use Storage;
use File;
use Excel;

/**
 * 荣誉上传
 * Class Upload
 * @package App\Http\Controllers\Admin\Upload
 */
class UploadController extends BaseController
{
    protected $service;

    public function __construct()
    {
    	parent::__construct();
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('themes.admin.upload.index');
    }

    // 教学建设成果上传界面路由
    public function ConstructionUploadIndex() {
        return view('themes.admin.upload.construction');
    }

    // 教学建设Excel上传界面路由
    public function uploadConstrExcel() {
        return view('themes.admin.upload.uploadConstrExcel');
    }

    // 教学成果excel表格式上传获奖信息
    public function uploadExcel() {
        return view('themes.admin.upload.uploadexcel');
    }

    // 学术成果上传界面路由
    public function academicUploadIndex() {
        return view('themes.admin.upload.academic');
    }

    // 学术成果excel表格式上传获奖信息
    public function uploadAcademicExcel() {
        return view('themes.admin.upload.uploadAcademicExcel');
    }
    
    // 上传获奖信息
    public function uploadAward(Request $request) {
        // 获取所有的上传图片，经测试结果为数组
        $all_imgs = $request->file();
        $files_path = [];   
        foreach ($all_imgs as $img_item) {
            // 获取后缀名
            $img_item_ext = $img_item->extension();
            $img_item_oname = $img_item->getClientOriginalName();
            // 新的文件名
            $username = auth()->user()->jobnumber. "+" .auth()->user()->name;
            $username = iconv('utf-8','gbk',$username);
            $img_item_name = $img_item_oname;
            $img_item_name = iconv('utf-8','gbk',$img_item_name);
            // dd($img_item_name);
            // 使用store()进行文件的存储
            $path_res = $img_item->storeAs($username,$img_item_name);
            $path_name = $username.'/'.$img_item_name;
            $path_name = iconv('gbk', 'utf-8', $path_name); 
            array_push($files_path, $path_name);
        }   
        // 获取上传的图片的其他信息
        $awardInfo = [];    
        // 拼接图片路径地址
        $files_paths = "";
        foreach ($files_path as $file_item) {
            $files_paths = $files_paths.$file_item;
        }
        // 向awards表进行插入操作
        $awardInfo_all['user_id'] = auth()->user()->id;
        $awardInfo_all['name'] = auth()->user()->name;
        $awardInfo_all['jobnumber'] = auth()->user()->jobnumber;
        $awardInfo_all['award_content'] = $request->input('award_content');
        $awardInfo_all['award_category'] = $request->input('award_category');
        $awardInfo_all['award_time'] = $request->input('award_time');
        $awardInfo_all['award_level'] = $request->input('award_level');
        $awardInfo_all['award_prize'] = $request->input('award_prize');
        $awardInfo_all['award_money_amount'] = $request->input('award_money_amount');
        $awardInfo_all['award_calc_year'] = $request->input('award_calc_year');
        $awardInfo_all['allusers'] = $request->input('allusers');
        $awardInfo_all['award_img_url'] = $files_paths;
        $awardInfo_all['create_time'] = date('Y-m-d H:i:s',time());
        $res = DB::table('honor_awards')->insert($awardInfo_all);
        if ($res) {
            return response()->json(array(
                    'res' => 1
                ));
        }else {
            return response()->json(array(
                    'res' => 0
                ));
        }
    }

    // 上传excel获奖成果信息
    public function readAndStorage(Request $request) {
        // 获取所有的上传excel文件，经测试结果为数组
        $excel = $request->file();
        $res = true;
        foreach ($excel as $excelItem) {
            // $excelItem = iconv('GBK', 'UTF-8', $excelItem);
            Excel::load($excelItem, function ($reader) {            
                $data = $reader->all()->groupBy('items')->toArray();
                foreach ($data as $award_record) {
                    foreach ($award_record as $record) {
                        if ($record['jobnumber'] != "教职工编号" && $record['jobnumber'] != null) {
                            unset($record[0]);
                            $record['name'] = auth()->user()->name;
                            $record['user_id'] = auth()->user()->id;
                            $record['award_calc_year'] = '2018';
                            $record['create_time'] = date('Y-m-d H:i:s',time());
                            $res = DB::table('honor_awards')->insert($record);
                        }
                    }
                    
                }
            });

        }
        if ($res) {
            return response()->json(array(
                    'res' => 1
                ));
        }else {
            return response()->json(array(
                    'res' => 0
                ));
        }
    }

    // 上传教学建设信息
    public function uploadConstruction(Request $request) {
        // 获取上传的图片的其他信息
        $awardInfo = [];    
        // 向awards表进行插入操作
        $awardInfo_all['charge_name'] = auth()->user()->name;
        $awardInfo_all['jobnumber'] = auth()->user()->jobnumber;
        $awardInfo_all['department_name'] = auth()->user()->department_name;
        $awardInfo_all['project_id'] = $request->input('project_id');
        $awardInfo_all['project_name'] = $request->input('project_name');
        $awardInfo_all['category'] = $request->input('category');
        $awardInfo_all['level'] = $request->input('level');
        $awardInfo_all['place_level'] = $request->input('place_level');
        $awardInfo_all['create_year'] = $request->input('create_year');
        $awardInfo_all['proposed_fund'] = $request->input('proposed_fund');
        $awardInfo_all['allusers'] = $request->input('allusers');
        $awardInfo_all['hasfinished'] = $request->input('hasfinished');
        $awardInfo_all['create_time'] = date('Y-m-d H:i:s',time());
        $res = DB::table('honor_teaching_constructions')->insert($awardInfo_all);
        if ($res) {
            return response()->json(array(
                    'res' => 1
                ));
        }else {
            return response()->json(array(
                    'res' => 0
                ));
        }

    }

    // 上传excel教学建设信息
    public function readConstrExcel(Request $request) {
        // 获取所有的上传excel文件，经测试结果为数组
        $excel = $request->file();
        $res = true;
        foreach ($excel as $excelItem) {
            // $excelItem = iconv('GBK', 'UTF-8', $excelItem);
            Excel::load($excelItem, function ($reader) {            
                $data = $reader->all()->groupBy('items')->toArray();
                foreach ($data as $award_record) {
                    foreach ($award_record as $record) {
                        if ($record['project_id'] != "项目编号" && $record['project_id'] != null) {
                            unset($record[0]);
                            $record['department_name'] = auth()->user()->department_name;
                            $record['create_time'] = date('Y-m-d H:i:s',time());
                            if ($record['hasfinished'] == '是') $record['hasfinished'] = 1;
                            else $record['hasfinished'] = 0;
                            $res = DB::table('honor_teaching_constructions')->insert($record);
                        }
                    }
                    
                }
            });

        }
        if ($res) {
            return response()->json(array(
                    'res' => 1
                ));
        }else {
            return response()->json(array(
                    'res' => 0
                ));
        }
    }

    // 上传学术成果获奖信息
    public function uploadAcademic(Request $request) {
        // 获取所有的上传图片，经测试结果为数组
        $all_imgs = $request->file();
        $files_path = [];   
        foreach ($all_imgs as $img_item) {
            // 获取后缀名
            $img_item_ext = $img_item->extension();
            $img_item_oname = $img_item->getClientOriginalName();
            // 新的文件名
            $username = auth()->user()->jobnumber. "+" .auth()->user()->name;
            $username = iconv('utf-8','gbk',$username);
            $img_item_name = $img_item_oname;
            $img_item_name = iconv('utf-8','gbk',$img_item_name);
            // dd($img_item_name);
            // 使用store()进行文件的存储
            $path_res = $img_item->storeAs($username,$img_item_name);
            $path_name = $username.'/'.$img_item_name;
            $path_name = iconv('gbk', 'utf-8', $path_name); 
            array_push($files_path, $path_name);
        }   
        // 获取上传的图片的其他信息
        $awardInfo = [];    
        // 拼接图片路径地址
        $files_paths = "";
        foreach ($files_path as $file_item) {
            $files_paths = $files_paths.$file_item;
        }
        // 向awards表进行插入操作
        $awardInfo_all['first_author'] = $request->input('first_author');
        $awardInfo_all['jobnumber'] = $request->input('jobnumber');
        $awardInfo_all['academic_name'] = $request->input('academic_name');
        $awardInfo_all['academic_category'] = $request->input('academic_category');
        $awardInfo_all['specific_category'] = $request->input('specific_category');
        $awardInfo_all['publication'] = $request->input('publication');
        $awardInfo_all['published_date'] = $request->input('published_date');
        $awardInfo_all['other_mumbers'] = $request->input('other_mumbers');
        $awardInfo_all['is_transfered'] = $request->input('is_transfered');
        $awardInfo_all['certificate_url'] = $files_paths;
        $awardInfo_all['created_time'] = date('Y-m-d H:i:s',time());
        $res = DB::table('honor_academics')->insert($awardInfo_all);
        if ($res) {
            return response()->json(array(
                    'res' => 1
                ));
        }else {
            return response()->json(array(
                    'res' => 0
                ));
        }
    }

    // 上传excel获奖成果信息
    public function readAcademicExcel(Request $request) {
        // 获取所有的上传excel文件，经测试结果为数组
        $excel = $request->file();
        $res = true;
        foreach ($excel as $excelItem) {
            // $excelItem = iconv('GBK', 'UTF-8', $excelItem);
            Excel::load($excelItem, function ($reader) {            
                $data = $reader->all()->groupBy('items')->toArray();
                foreach ($data as $award_record) {
                    foreach ($award_record as $record) {
                        if ($record['jobnumber'] != "第一作者职工编号" && $record['jobnumber'] != null) {
                            unset($record[0]);
                            $record['created_time'] = date('Y-m-d H:i:s',time());
                            $res = DB::table('honor_academics')->insert($record);
                        }
                    }
                    
                }
            });

        }
        if ($res) {
            return response()->json(array(
                    'res' => 1
                ));
        }else {
            return response()->json(array(
                    'res' => 0
                ));
        }
    }
}